package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.MallDeliveryAddress;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.AddAddressDTO;
import com.sc.nft.entity.dto.ModificationAddRessDTO;
import com.sc.nft.entity.vo.MallDeliveryAddressVO;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallDeliveryAddressService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mallAddress")
@Api(tags = "商城收货地址")
public class MallDeliveryAddressController {

    private final MallDeliveryAddressService mallDeliveryAddressService;

    @ApiOperation(value = "新增地址", notes = "新增地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/addAddress", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result addAddress(@Valid @RequestBody AddAddressDTO addAddressDTO) {
        mallDeliveryAddressService.addAddress(addAddressDTO, UserPool.getUserId());
        return Result.success();
    }

    @ApiOperation(value = "修改地址", notes = "修改地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/modificationAddress", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result modificationAddress(@Valid @RequestBody ModificationAddRessDTO modificationAddRessDTO) {
        mallDeliveryAddressService.modificationAddress(modificationAddRessDTO,  UserPool.getUserId());
        return Result.success();
    }


    @ApiOperation(value = "根据id获取地址信息", notes = "根据id获取地址信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getDataById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MallDeliveryAddressVO> getDataById(@Valid @RequestBody IdRequest idRequest) {
        return Result.success(mallDeliveryAddressService.getDataById(idRequest.getId()));
    }


    @ApiOperation(value = "根据id删除地址", notes = "根据id删除地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result deleteById(@Valid @RequestBody IdRequest idRequest) {
        MallDeliveryAddress address = mallDeliveryAddressService.getAddressByUserIdAndId( UserPool.getUserId(), idRequest.getId());
        if (ObjectUtil.isNull(address)) {
            return Result.fail("地址不存在");
        }
        address.deleteById();
        return Result.success();
    }

    @ApiOperation(value = "分页查询地址列表", notes = "分页查询地址列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MallDeliveryAddressVO> page(@Valid @RequestBody PageRequest pageRequest) {
        return PageResult.success(mallDeliveryAddressService.page(pageRequest, UserPool.getUserId()));
    }

    @ApiOperation(value = "获取默认地址", notes = "获取默认地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getDefaultAddress", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MallDeliveryAddressVO> getDefaultAddress() {
        MallDeliveryAddressVO defaultAddress = mallDeliveryAddressService.getDefaultAddress(UserPool.getUserId());
        if (BeanUtil.isEmpty(defaultAddress)) {
            return Result.success();
        }
        defaultAddress.setDefaultAddress(true);
        return Result.success(defaultAddress);
    }
}
